package com.educate.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.educate.serviceedu.client.VodClient;
import com.educate.serviceedu.entity.EduVideo;
import com.educate.serviceedu.mapper.EduVideoMapper;
import com.educate.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author educate.wht
 * @since 2022-08-16
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;
    //根据课程id删小节
    @Override
    public void removeVideoByCourseId(String courseId) {
        //根据课程id查询所有小节id
        QueryWrapper<EduVideo> wrapperOne=new QueryWrapper<>();
        wrapperOne.eq("course_id",courseId);
        List<EduVideo> eduVideos = baseMapper.selectList(wrapperOne);
        wrapperOne.select("video_source_id");
        List<String> videoList=new ArrayList<>();
        for (int i = 0; i < eduVideos.size(); i++) {
            EduVideo eduVideo = eduVideos.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                //方法videoList集合中去
                videoList.add(videoSourceId);
            }
        }
        if(videoList.size()>0){
            vodClient.deleteBath(videoList);
        }
        QueryWrapper<EduVideo> wrapper=new QueryWrapper();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
